﻿// Decompiled with JetBrains decompiler
// Type: Netick.ExecutionList
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;
using System.Collections.Generic;

#nullable disable
namespace Netick;

public sealed class ExecutionList
{
  private NetickEngine Engine;
  private List<ExecutionList.BehaviourList> BehaviourNodes = new List<ExecutionList.BehaviourList>(64 /*0x40*/);
  private Dictionary<Type, ExecutionList.BehaviourList> TypeToBehaviourChain = new Dictionary<Type, ExecutionList.BehaviourList>(64 /*0x40*/);

  public ExecutionList(NetickEngine engine)
  {
    this.Engine = engine;
    for (int index = 0; index < this.Engine._reflectionData.NetickBehaviourTypes.Count; ++index)
    {
      Type netickBehaviourType = this.Engine._reflectionData.NetickBehaviourTypes[index];
      ExecutionList.BehaviourList behaviourList = new ExecutionList.BehaviourList(this.Engine.GetBehaviourOrder(netickBehaviourType), 100);
      this.TypeToBehaviourChain.Add(netickBehaviourType, behaviourList);
      this.BehaviourNodes.Add(behaviourList);
      this.BehaviourNodes.Sort((Comparison<ExecutionList.BehaviourList>) ((x, y) => x.Order.CompareTo(y.Order)));
    }
  }

  public void Clear()
  {
    foreach (ExecutionList.BehaviourList behaviourNode in this.BehaviourNodes)
      behaviourNode.List.Clear();
  }

  public void SetFrom(List<Entity> list)
  {
    this.Clear();
    for (int index = 0; index < list.Count; ++index)
      this.Add(list[index]);
  }

  internal void Add(Entity obj)
  {
    foreach (INetickScript allScript in obj.UserEntity.AllScripts)
      this.AddBehaviour(allScript);
  }

  public void Remove(Entity obj)
  {
    foreach (INetickScript allScript in obj.UserEntity.AllScripts)
      this.RemoveBehaviour(allScript);
  }

  internal void RemoveBehaviour(INetickScript behaviour)
  {
    this.TypeToBehaviourChain[behaviour.GetType()].List.Remove(behaviour);
  }

  internal void AddBehaviour(INetickScript newBehaviour)
  {
    Type type = newBehaviour.GetType();
    if (this.TypeToBehaviourChain.ContainsKey(type))
    {
      this.TypeToBehaviourChain[type].List.Add(newBehaviour);
    }
    else
    {
      ExecutionList.BehaviourList behaviourList = new ExecutionList.BehaviourList(this.Engine.GetBehaviourOrder(type), 100);
      this.TypeToBehaviourChain.Add(type, behaviourList);
      this.BehaviourNodes.Add(behaviourList);
      this.BehaviourNodes.Sort((Comparison<ExecutionList.BehaviourList>) ((x, y) => x.Order.CompareTo(y.Order)));
      behaviourList.List.Add(newBehaviour);
    }
  }

  internal void ExecuteNetworkStartAndClear()
  {
    foreach (ExecutionList.BehaviourList behaviourNode in this.BehaviourNodes)
    {
      for (int index = 0; index < behaviourNode.List.Count; ++index)
      {
        try
        {
          behaviourNode.List[index].NetworkStart();
        }
        catch (Exception ex)
        {
          ExecutionList.BehaviourList targetScript = behaviourNode;
          NetickLogger.LogError((object) ex, (object) targetScript);
        }
      }
    }
    this.Clear();
    Action postNetworkStart = this.Engine.OnPostNetworkStart;
    if (postNetworkStart == null)
      return;
    postNetworkStart();
  }

  internal void ExecuteNetworkFixedUpdate()
  {
    for (int index1 = 0; index1 < this.BehaviourNodes.Count; ++index1)
    {
      ExecutionList.BehaviourList behaviourNode = this.BehaviourNodes[index1];
      for (int index2 = 0; index2 < behaviourNode.List.Count; ++index2)
      {
        try
        {
          behaviourNode.List[index2].NetworkFixedUpdate();
        }
        catch (Exception ex)
        {
          ExecutionList.BehaviourList targetScript = behaviourNode;
          NetickLogger.LogError((object) ex, (object) targetScript);
        }
      }
    }
  }

  internal void ExecuteNetworkUpdate()
  {
    foreach (ExecutionList.BehaviourList behaviourNode in this.BehaviourNodes)
    {
      for (int index = 0; index < behaviourNode.List.Count; ++index)
      {
        try
        {
          behaviourNode.List[index].NetworkUpdate();
        }
        catch (Exception ex)
        {
          ExecutionList.BehaviourList targetScript = behaviourNode;
          NetickLogger.LogError((object) ex, (object) targetScript);
        }
      }
    }
  }

  internal void ExecuteNetworkRender()
  {
    foreach (ExecutionList.BehaviourList behaviourNode in this.BehaviourNodes)
    {
      for (int index = 0; index < behaviourNode.List.Count; ++index)
      {
        try
        {
          behaviourNode.List[index].NetworkRender();
        }
        catch (Exception ex)
        {
          ExecutionList.BehaviourList targetScript = behaviourNode;
          NetickLogger.LogError((object) ex, (object) targetScript);
        }
      }
    }
  }

  internal void ExecuteGameEngineIntoNetcode()
  {
    foreach (ExecutionList.BehaviourList behaviourNode in this.BehaviourNodes)
    {
      for (int index = 0; index < behaviourNode.List.Count; ++index)
      {
        try
        {
          behaviourNode.List[index].GameEngineIntoNetcode();
        }
        catch (Exception ex)
        {
          ExecutionList.BehaviourList targetScript = behaviourNode;
          NetickLogger.LogError((object) ex, (object) targetScript);
        }
      }
    }
  }

  internal void ExecuteNetcodeIntoGameEngine()
  {
    foreach (ExecutionList.BehaviourList behaviourNode in this.BehaviourNodes)
    {
      for (int index = 0; index < behaviourNode.List.Count; ++index)
      {
        try
        {
          behaviourNode.List[index].NetcodeIntoGameEngine();
        }
        catch (Exception ex)
        {
          ExecutionList.BehaviourList targetScript = behaviourNode;
          NetickLogger.LogError((object) ex, (object) targetScript);
        }
      }
    }
  }

  private class BehaviourList
  {
    public List<INetickScript> List;
    public int Order;

    public BehaviourList(int order, int cap)
    {
      this.List = new List<INetickScript>(cap);
      this.Order = order;
    }
  }
}
